package com.qfedu.dao.impl;

import com.qfedu.dao.ICartDao;
import com.qfedu.entity.Cart;
import com.qfedu.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements ICartDao {
    @Override
    public int insertCart(int uid, int pid, double money) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "insert into cart(uid,pid,count,money) values(?,?,?,?)", uid, pid, 1,money);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCart(int uid, int pid) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update cart set count = count +1 where uid = ? and pid = ?", uid, pid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public Cart checkCart(int uid, int pid) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select uid,pid from cart where uid = ? and pid = ?",new BeanHandler<Cart>(Cart.class),uid,pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Cart> listCart(int uid) {
        try {
            return new QueryRunner(JDBCUtil.getDataSource()).query("select uid,pid,count,money from cart where uid = ?",new BeanListHandler<Cart>(Cart.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int subtractCart(int uid, int pid) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update cart set count = count - 1 where uid = ? and pid = ?", uid, pid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int changeCartCount(int uid, int pid, int count) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "update cart set count = ? where uid = ? and pid = ?",count, uid, pid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteCart(int uid, int pid) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "delete from cart where uid = ? and pid = ?", uid, pid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int clearCart(int uid) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            int update = queryRunner.update(JDBCUtil.getConnection(), "delete from cart where uid = ?", uid);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
